package io.renren;

import java.util.concurrent.*;

public class ThreadTest {

    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main开始");
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        }, executorService);
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executorService).whenComplete((res,exception)->{
            System.out.println("异步任务完成了，结果是："+res+";异常是："+exception);
//        }).exceptionally(throwable -> {
//            return 10;
        }).handle((res,ex)->{
            if(res!=null){
                return 1000;
            }
            if(ex!=null){
                return 9999;
            }
            return -1;
        });
        System.out.println("main结束"+future.get());
    }
}
